package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;


import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Reference
    private OrderService orderService;


    /**
     * 预约列表中根据条件查询分页的方法
     * @param queryPageBean
     * @param orderQueryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean, OrderQueryPageBean orderQueryPageBean){

        String orderType = orderQueryPageBean.getOrderType();
        Date minTime = orderQueryPageBean.getMinTime();
        Date maxTime = orderQueryPageBean.getMaxTime();

        try {
            PageResult page = orderService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString(), orderType, minTime, maxTime);
            if (page==null){
                return  new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
            }
            System.out.println(page);
            return  new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,new PageResult(page.getTotal(),page.getRows()));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
    }


    /**
     * 预约列表中修改或者添加的方法
     * 根据由没有id 进行判断
     * @param order
     * @return
     */
    @PostMapping("submit")
    Result updateOrSave(@RequestBody Order order){
        try {
            if (order.getId()!=null){
                int i = orderService.updateById(order);
                if (i==0){
                    return new Result(false,MessageConstant.ORDERSETTING_FAIL);
                }
                return new Result(true,MessageConstant.ORDERSETTING_SUCCESS) ;
            }
            int save = orderService.save(order);
            if (save==0){
                return new Result(false,MessageConstant.ORDERSETTING_FAIL);
            }
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS) ;
        } catch (Exception e) {
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 预约列表中根据id查询单个对象信息的方法
     * @param id
     * @return
     */
    @GetMapping("{id}")
    Result findById(@PathVariable("id")String id){
        Order order = orderService.findById(id);
        if (order==null){
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,order);
    }


    /**
     *到诊未到诊
     */
//    @GetMapping("/{id}/{arrival}")
//    public Result update(@PathVariable("id") String id ,@PathVariable("arrival") Integer arrival){
//
//        try {
//            boolean bool = orderService.update(id,arrival);
//
//            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS);
//        } catch (Exception e) {
//            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
//        }
//    }
    @GetMapping("/{id}/{arrival}")
    public void update(@PathVariable("id") String id ,@PathVariable("arrival") Integer arrival){
            boolean bool = orderService.update(id,arrival);
    }

}